//
//  LYIntroduceModel.m
//  LYLiveApp
//
//  Created by QD on 16/9/2.
//  Copyright © 2016年 览益信息科技. All rights reserved.
//

#import "LYIntroduceModel.h"
#import "LYUtil.h"
#import "VideoViewContants.h"
#import <ImageIO/ImageIO.h>
#import "SDImageCache.h"

@implementation LYIntroduceModel

@end

@implementation LYRoomListModel
+(NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass
{
    return @{@"teachers" : LYTeacherModel.class};
}

+ (CGFloat)returnRowHeightWithModel:(LYRoomListModel *)model {
    
    UIFont *font = [UIFont fontWithName:@"PingFangSC-Regular" size:15.0f];

    if (model.actStatus.integerValue == 1) {
       
        if (model.actType.integerValue == 2) {
            CGFloat labelHeight = [LYUtil sizeWithText:model.foreshow font:font withWidth:SCREEN_WIDTH - 28].height + 10;
            if (labelHeight < 20) {
                labelHeight = 16;
            }
            CGFloat contentHeight = 0;
            for (int i = 0; i < model.teachers.count; i ++) {
                LYTeacherModel *teacherModel = model.teachers[i];
                CGFloat height = [LYUtil sizeWithText:teacherModel.desc font:font withWidth:SCREEN_WIDTH - 62].height + 55 + 10;
                contentHeight += height;
            }
            
            if (!model.foreshow.length && !model.teachers.count) {
                return 45;
            }
            
            return labelHeight + contentHeight + 60;

        } else {
            CGFloat labelHeight = [LYUtil sizeWithText:model.foreshow font:font withWidth:SCREEN_WIDTH - 65].height;
            if (labelHeight < 20) {
                labelHeight = 16;
            }
            if (!model.foreshow.length) {
                labelHeight = 0;
            }
            
            CGFloat contentHeight = 0;
            for (int i = 0; i < model.teachers.count; i ++) {
                LYTeacherModel *teacherModel = model.teachers[i];
                CGFloat height = [LYUtil sizeWithText:teacherModel.desc font:font withWidth:SCREEN_WIDTH - 62].height + 55 + 10;
                contentHeight += height;
            }
            return labelHeight + contentHeight + 70;
        }

    } else {
        CGFloat contentHeight = [LYUtil sizeWithText:model.foreshow font:font withWidth:SCREEN_WIDTH - 65].height;
        if (contentHeight < 20) {
            contentHeight = 16;
        }
        return contentHeight + 68;
    }
    
}

@end

@implementation LYArticleModel

+ (CGFloat)returnRowHeightWithModel:(LYArticleModel *)model {
        CGFloat height = [LYUtil sizeWithText:model.article_content
                                         font:[UIFont systemFontOfSize:kArticleContentFontSize]
                                        width:SCREEN_WIDTH - kArticleDesreaseWidth
                                  lineSpacing: kArticleContentLineSpacing].height + kArticleOtherViewHeight;
    return height;
}
@end

@implementation LYTimeModel

- (void)prepareHeight {
    
    if (self.height > 0 || [LYUtil isNilOrEmpty:self.img_url])      return;
    
        NSURL * url = [NSURL URLWithString:self.img_url];
        UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:self.img_url];
    if (cachedImage != nil) {
        self.height = self.height = SCREEN_WIDTH * cachedImage.size.height / cachedImage.size.width;
        return;
    }
        CGImageSourceRef imageSourceRef =     CGImageSourceCreateWithURL((CFURLRef)url, NULL);
        CGFloat width = 0, height = 0;
        if (imageSourceRef) {
            CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);
            if (imageProperties != NULL) {
                CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
                if (widthNumberRef != NULL) {
                    CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
                }
                CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
                if (heightNumberRef != NULL) {
                    CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
                }
                CFRelease(imageProperties);
            }
            CFRelease(imageSourceRef);
        }
    if (width == 0) {
        width = SCREEN_WIDTH;
    }
    self.height = SCREEN_WIDTH * height / width;    
}


- (NSMutableArray *)playChannels
{
    if (_playChannels == nil) {
        _playChannels = [NSMutableArray arrayWithCapacity:3];
    }
    return _playChannels;
}
@end
@implementation LYTimeTableTeacherModel
{
    CGFloat _height;
    CGFloat _styleHeight;
}
//- (CGFloat)height
//{
//    if (_height == 0) {
//        CGFloat teacherTop = 59;
//        CGFloat teacherPadding = 5;
//        
//        _styleHeight = [self.style boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-80, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFang-SC-Regular" size:13]} context:nil].size.height;
//        
//        CGFloat teacherDescHeight = [self.desc boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20*2, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFang-SC-Medium" size:14]} context:nil].size.height;
//        teacherDescHeight += 6;
//        _height = teacherTop + teacherDescHeight + teacherPadding*2 + 10+10+_styleHeight - 14;
//    }
//    return _height;
//}

@end
@implementation LYTimePlayItemModel
{
    CGFloat _height;
    CGFloat _descAttrHeight;
    NSMutableAttributedString *_descAttrText;
    
}
+(NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass
{
    return @{@"teachers" : LYTimeTableTeacherModel.class};
}

+(CGFloat)returnHeightWithModel:(LYTimePlayItemModel *)model {

    NSString *nameStr;
    CGFloat modelHeight = 0;
    UIFont *font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15.0f];

    if (model.act_status.integerValue == 1) {
        CGFloat teacherCellHeight = 0;
        for (int i = 0; i < model.teachers.count; i ++) {
            LYTimeTableTeacherModel *teacherModel = model.teachers[i];
            CGFloat contentHeight = [LYUtil sizeWithText:teacherModel.desc font:font withWidth:SCREEN_WIDTH - 70].height;
            if (model.isIMOpen) {
                teacherModel.height = contentHeight + 138;
            } else {
                teacherModel.height = contentHeight + 138 - 40;
            }
            teacherCellHeight += (teacherModel.height + 10);
        }
        
        modelHeight = teacherCellHeight;
    }
    
    if (model.teachers.count) {
        LYTimeTableTeacherModel *teacherOne = model.teachers[0];
        nameStr = [NSString stringWithFormat:@"%@:",teacherOne.nickname];
    }
    if (model.teachers.count > 1) {
        LYTimeTableTeacherModel *teacherOne = model.teachers[0];
        LYTimeTableTeacherModel *teacherTwo = model.teachers[1];
        nameStr = [NSString stringWithFormat:@"%@/%@:",teacherOne.nickname,teacherTwo.nickname];
    }
    NSString *contentString = [NSString stringWithFormat:@"%@%@",nameStr,model.foreshow];
    CGFloat contentLabelHeight = [LYUtil sizeWithText:contentString font:font withWidth:SCREEN_WIDTH - 53].height;
    return contentLabelHeight + 85 + modelHeight;

}



//- (CGFloat)height
//{
//    if (_height == 0) {
//        
//        NSString *preText = @"";
//        if (self.act_status.integerValue == 0) {
//            preText = @"预告：";
//        }else{
//            preText = @"本期：";
//        }
//        NSString *desc = [NSString stringWithFormat:@"%@%@",preText,self.foreshow];
//        NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:desc];
//        [attrText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang-SC-Medium" size:13] range:NSRangeFromString(preText)];
//        [attrText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang-SC-Regular" size:13] range:NSMakeRange(preText.length, attrText.string.length - preText.length)];
//        _descAttrText = attrText;
//        _descAttrHeight = [attrText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-10*2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size.height;
//        _descAttrHeight += 10;
//        if (_descAttrHeight <= 20) {
//            _descAttrHeight = 20;
//        }
//        
//        CGFloat topMargin = 36;
//        if (self.act_status.integerValue == 1) { // 正在直播
//            _height = topMargin + _descAttrHeight + 10;
//            
//            for (LYTimeTableTeacherModel *teacherModel in self.teachers) {                
//                _height = _height + teacherModel.height;
//            }
//            
//        }else{
//            _height = topMargin + _descAttrHeight + 10;
//        }
//        
//    }
//    return _height;
//}
- (BOOL)isIMOpen
{
    if (self.im_status.integerValue == 1) {
        return YES;
    }else{
        return NO;
    }
}
- (CGFloat)descAttrHeight
{
    return _descAttrHeight;
}
@end
